package com.openbravo.pos.payment;

import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.AppProperties;
import com.openbravo.pos.util.AltEncrypter;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class PaymentGatewayPGNET
  implements PaymentGateway
{
  private static String ENDPOINTADDRESS;
  private static final String SALE = "10";
  private static final String REFUND = "13";
  private static final String APPROVED = "A";
  private String m_sCommerceID;
  private String m_sCommercePassword;
  private boolean m_bTestMode;

  public PaymentGatewayPGNET(AppProperties paramAppProperties)
  {
    this.m_sCommerceID = paramAppProperties.getProperty("payment.commerceid");
    AltEncrypter localAltEncrypter = new AltEncrypter("cypherkey" + paramAppProperties.getProperty("payment.commerceid"));
    this.m_sCommercePassword = localAltEncrypter.decrypt(paramAppProperties.getProperty("payment.commercepassword").substring(6));
    this.m_bTestMode = Boolean.valueOf(paramAppProperties.getProperty("payment.testmode")).booleanValue();
    ENDPOINTADDRESS = this.m_bTestMode ? "https://www.paymentsgateway.net/cgi-bin/posttest.pl" : "https://www.paymentsgateway.net/cgi-bin/postauth.pl";
  }

  public PaymentGatewayPGNET()
  {
  }

  public void execute(PaymentInfoMagcard paramPaymentInfoMagcard)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    try
    {
      localStringBuffer.append("pg_merchant_id=");
      localStringBuffer.append(this.m_sCommerceID);
      localStringBuffer.append("&pg_password=");
      localStringBuffer.append(this.m_sCommercePassword);
      localStringBuffer.append("&pg_total_amount=");
      DecimalFormat localDecimalFormat = new DecimalFormat("0000.00");
      String str1 = localDecimalFormat.format(Math.abs(paramPaymentInfoMagcard.getTotal()));
      localStringBuffer.append(URLEncoder.encode(str1.replace(',', '.'), "UTF-8"));
      if (paramPaymentInfoMagcard.getTrack1(true) != null)
      {
        localStringBuffer.append("&pg_cc_swipe_data=");
        localStringBuffer.append(URLEncoder.encode(paramPaymentInfoMagcard.getTrack1(true), "UTF-8"));
      }
      else
      {
        localStringBuffer.append("&ecom_payment_card_type=");
        localStringBuffer.append(getCardType(paramPaymentInfoMagcard.getCardNumber()));
        localStringBuffer.append("&ecom_payment_card_number=");
        localStringBuffer.append(URLEncoder.encode(paramPaymentInfoMagcard.getCardNumber(), "UTF-8"));
        localStringBuffer.append("&ecom_payment_card_expdate_month=");
        String localObject1 = paramPaymentInfoMagcard.getExpirationDate();
        localStringBuffer.append(((String)localObject1).substring(0, 2));
        localStringBuffer.append("&ecom_payment_card_expdate_year=");
        localStringBuffer.append(((String)localObject1).substring(2, ((String)localObject1).length()));
        String[] localObject2 = paramPaymentInfoMagcard.getHolderName().split(" ");
        localStringBuffer.append("&ecom_billto_postal_name_first=");
        if (localObject2.length > 0)
          localStringBuffer.append(URLEncoder.encode(localObject2[0], "UTF-8"));
        localStringBuffer.append("&ecom_billto_postal_name_last=");
        if (localObject2.length > 1)
          localStringBuffer.append(URLEncoder.encode(localObject2[1], "UTF-8"));
        localStringBuffer.append("&ecom_payment_card_name=");
        localStringBuffer.append(paramPaymentInfoMagcard.getHolderName());
      }
      if (paramPaymentInfoMagcard.getTotal() >= 0.0D)
      {
        localStringBuffer.append("&pg_transaction_type=");
        localStringBuffer.append("10");
      }
      else
      {
        localStringBuffer.append("&pg_transaction_type=");
        localStringBuffer.append("13");
      }
      localStringBuffer.append("&endofdata");
      Object localObject1 = new URL(ENDPOINTADDRESS);
      Object localObject2 = ((URL)localObject1).openConnection();
      ((URLConnection)localObject2).setDoOutput(true);
      ((URLConnection)localObject2).setUseCaches(false);
      ((URLConnection)localObject2).setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      DataOutputStream localDataOutputStream = new DataOutputStream(((URLConnection)localObject2).getOutputStream());
      localDataOutputStream.write(localStringBuffer.toString().getBytes());
      localDataOutputStream.flush();
      localDataOutputStream.close();
      BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(((URLConnection)localObject2).getInputStream()));
      String str3;
      for (String str2 = ""; (str3 = localBufferedReader.readLine()) != null; str2 = str2 + "&" + str3);
      String str2 = null;
	paramPaymentInfoMagcard.setReturnMessage(str2);
      localBufferedReader.close();
      HashMap localHashMap = new HashMap();
      StringTokenizer localStringTokenizer = new StringTokenizer(str2, "&");
      String str4;
      while (localStringTokenizer.hasMoreTokens())
      {
        str4 = localStringTokenizer.nextToken();
        int i = str4.indexOf(61);
        if (i >= 0)
          localHashMap.put(URLDecoder.decode(str4.substring(0, i), "UTF-8"), URLDecoder.decode(str4.substring(i + 1), "UTF-8"));
        else
          localHashMap.put(URLDecoder.decode(str4, "UTF-8"), null);
      }
      if ("A".equals(localHashMap.get("pg_response_type")))
      {
        paramPaymentInfoMagcard.paymentOK((String)localHashMap.get("pg_authorization_code"), (String)localHashMap.get("pg_trace_number"), str2);
      }
      else
      {
        str4 = (String)localHashMap.get("pg_response_description");
        str4 = str4.replace("F01", "\nMANDITORY FIELD MISSING");
        str4 = str4.replace("F03", "\nINVALID FIELD NAME");
        str4 = str4.replace("F04", "\nINVALID FIELD VALUE");
        str4 = str4.replace("F05", "\nDUPLICATE FIELD");
        str4 = str4.replace("F07", "\nCONFLICTING FIELD");
        paramPaymentInfoMagcard.paymentError(AppLocal.getIntString("message.paymenterror"), str4);
      }
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      paramPaymentInfoMagcard.paymentError(AppLocal.getIntString("message.paymentexceptionservice"), localUnsupportedEncodingException.getMessage());
    }
    catch (MalformedURLException localMalformedURLException)
    {
      paramPaymentInfoMagcard.paymentError(AppLocal.getIntString("message.paymentexceptionservice"), localMalformedURLException.getMessage());
    }
    catch (IOException localIOException)
    {
      paramPaymentInfoMagcard.paymentError(AppLocal.getIntString("message.paymenterror"), localIOException.getMessage());
    }
  }

  private String getCardType(String paramString)
  {
    String str = "UNKNOWN";
    if (paramString.startsWith("4"))
      str = "VISA";
    else if (paramString.startsWith("6"))
      str = "DISC";
    else if (paramString.startsWith("5"))
      str = "MAST";
    else if ((paramString.startsWith("34")) || (paramString.startsWith("37")))
      str = "AMER";
    else if ((paramString.startsWith("3528")) || (paramString.startsWith("3589")))
      str = "JCB";
    else if (paramString.startsWith("3"))
      str = "DINE";
    return str;
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.payment.PaymentGatewayPGNET
 * JD-Core Version:    0.6.0
 */